package com.yuexun.beilunpatient.ui.question.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.yuexun.beilunpatient.R;
import com.yuexun.beilunpatient.base.BaseEntity;
import com.yuexun.beilunpatient.base.BaseKJActivity;
import com.yuexun.beilunpatient.config.AppConfig;
import com.yuexun.beilunpatient.ui.main.bean.AreaBean;
import com.yuexun.beilunpatient.ui.main.bean.AreaHospBean;
import com.yuexun.beilunpatient.ui.main.model.impl.AreaHospModel;
import com.yuexun.beilunpatient.ui.main.presenter.impl.AreaHospPresenter;
import com.yuexun.beilunpatient.ui.main.ui.view.IAreaHospView;
import com.yuexun.beilunpatient.ui.question.bean.QuestionTypeBean;
import com.yuexun.beilunpatient.ui.question.model.impl.QuestionModel;
import com.yuexun.beilunpatient.ui.question.presenter.impl.QuestionAddPresenter;
import com.yuexun.beilunpatient.ui.question.ui.view.IQuestionAddView;
import com.yuexun.beilunpatient.widget.ActionSheetDialog;
import com.yuexun.beilunpatient.widget.LoadingDialog;

import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by chenss on 2018/3/22 0022.
 */

public class Act_Question_Add extends BaseKJActivity implements IQuestionAddView, IAreaHospView {

    @Bind(R.id.type_name)
    TextView typeName;
    @Bind(R.id.organization_name)
    TextView organizationName;
    @Bind(R.id.question_content)
    EditText questionContent;

    QuestionAddPresenter presenter;
    AreaHospPresenter areaHospPresenter;
    List<QuestionTypeBean> typeBeans;
    List<AreaHospBean> areaHospitals = new ArrayList<>();
    int typeId;
    int hospId;
    LoadingDialog loadingDialog;

    @Override
    public void setRootView() {
        setContentView(R.layout.act_question_add);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        loadingDialog = new LoadingDialog(aty);
        presenter = new QuestionAddPresenter(new QuestionModel(), this);
        areaHospPresenter = new AreaHospPresenter(new AreaHospModel(), this);
        Map<String, String> params = new HashMap<>();
        params.put("sessionUuidPatient", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
        presenter.inquirePatientQuestionTypeList(params);
        Map<String, String> params2 = new HashMap<>();
        areaHospPresenter.inquireAreaWithHospList(params2);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.title_back, R.id.type_ll, R.id.organization_ll, R.id.submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                finish();
                break;
            case R.id.type_ll:
                if(typeBeans != null && typeBeans.size() > 0){
                    ActionSheetDialog actionSheetDialog;
                    actionSheetDialog = new ActionSheetDialog(this).builder()
                            .setCancelable(true)
                            .setCanceledOnTouchOutside(true);

                    for (int i = 0; i < typeBeans.size(); i++) {
                        actionSheetDialog.addSheetItem(typeBeans.get(i).getQuestionType(), ActionSheetDialog.SheetItemColor.Blue,
                                new ActionSheetDialog.OnSheetItemClickListener() {
                                    @Override
                                    public void onClick(int which) {
                                        typeName.setText(typeBeans.get(which-1).getQuestionType());
                                        typeId = typeBeans.get(which-1).getTypeId();
                                    }
                                });
                    }
                    actionSheetDialog.show();
                }else{
                    ViewInject.toast("没用类型");
                }
                break;
            case R.id.organization_ll:
                if(areaHospitals != null && areaHospitals.size() > 0){
                    ActionSheetDialog actionSheetDialog;
                    actionSheetDialog = new ActionSheetDialog(this).builder()
                            .setCancelable(true)
                            .setCanceledOnTouchOutside(true);

                    for (int i = 0; i < areaHospitals.size(); i++) {
                        actionSheetDialog.addSheetItem(areaHospitals.get(i).getHospitalName(), ActionSheetDialog.SheetItemColor.Blue,
                                new ActionSheetDialog.OnSheetItemClickListener() {
                                    @Override
                                    public void onClick(int which) {
                                        organizationName.setText(areaHospitals.get(which-1).getHospitalName());
                                        hospId = areaHospitals.get(which-1).getId();
                                    }
                                });
                    }
                    actionSheetDialog.show();
                }
                break;
            case R.id.submit:
                if(typeName.getText().toString().isEmpty()){
                    ViewInject.toast("请选择类型");
                    break;
                }
                if(organizationName.getText().toString().isEmpty()){
                    ViewInject.toast("请选择医疗机构");
                    break;
                }
                if(questionContent.getText().toString().isEmpty()){
                    ViewInject.toast("请输入提问内容");
                    break;
                }
                loadingDialog.show();
                Map<String, String> params = new HashMap<>();
                params.put("sessionUuidPatient", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
                params.put("content", questionContent.getText().toString());
                params.put("hospName", organizationName.getText().toString());
                params.put("hospId", hospId + "");
                params.put("type", typeName.getText().toString());
                params.put("typeId", typeId + "");
                presenter.createPatientQuestion(params);
                break;
        }
    }

    @Override
    public void showQuestionType(BaseEntity<QuestionTypeBean> entity) {
        if(entity.getStatus().equals("200")){
            typeBeans = entity.getDatas();
        }
    }

    @Override
    public void createPatientQuestion(BaseEntity entity) {
        loadingDialog.dismiss();
        if(entity.getStatus().equals("200")){
            finish();
        }else{
            ViewInject.toast(entity.getMessage());
        }
    }

    @Override
    public void getAreaWithHospList(BaseEntity<AreaBean> baseEntity) {
        if(baseEntity.getStatus().equals("200")){
            for(AreaBean areaBean : baseEntity.getDatas()){
                areaHospitals.addAll(areaBean.getHospList());
            }
        }
    }

    @Override
    public void fail() {
        loadingDialog.dismiss();
    }
}
